package com.zzyl.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.ResourceDto;
import com.zzyl.entity.Resource;
import com.zzyl.service.ResourceService;
import com.zzyl.vo.MenuVo;
import com.zzyl.vo.TreeVo;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@Slf4j
@Api(tags = "资源权限管理")
@RequestMapping("/resource")
public class ResourceController {
    @Autowired
    private ResourceService resourceService;

    /**
     * 查询资源列表
     * @param param
     * @return
     */
    @PostMapping("/list")
    public ResponseResult<List<Resource>> getList(@RequestBody Map<String,String> param){
        log.info("查询资源列表接口，入参param={}",param);
//        if (param == null || param.size() == 0) {
//            param.put("resourceType",null);
//            param.put("parentResourceNo",null);
//        }
        List<Resource> resources = resourceService.getList(param);
        return ResponseResult.success(resources);
    }

    /**
     * 查询资源树形
     * @param resourceDto
     * @return
     */
    @PostMapping("/tree")
    @ApiOperationSupport(includeParameters = {"resourceDto.label"})
    public ResponseResult<TreeVo> getResourceAll(@RequestBody ResourceDto resourceDto){
        log.info("查询资源树形列表，入参ResourceDto={}",resourceDto);
        TreeVo treeVo = resourceService.getResourceAll(resourceDto);
        return ResponseResult.success(treeVo);
    }

    /**
     * 资源添加
     * @param resourceDto
     * @return
     */
    @PutMapping
    @ApiOperationSupport(includeParameters = {"resourceDto.dataState", "resourceDto.icon", "resourceDto.parentResourceNo"
            , "resourceDto.requestPath", "resourceDto.resourceName", "resourceDto.resourceType"
            , "resourceDto.sortNo"})
    public ResponseResult addResource(@RequestBody ResourceDto resourceDto){
        log.info("资源添加接口，入参resourceDto={}",resourceDto);
        resourceService.addResource(resourceDto);
        return ResponseResult.success();
    }

    /**
     * 资源修改
     * @return
     */
    @PatchMapping
    @ApiOperationSupport(includeParameters = {"resourceDto.dataState", "resourceDto.icon", "resourceDto.parentResourceNo"
            , "resourceDto.requestPath", "resourceDto.resourceName", "resourceDto.resourceType"
            , "resourceDto.sortNo","resourceDto.id"})
    public ResponseResult updateResource(@RequestBody ResourceDto resourceDto){
        log.info("资源修改接口，入参resourceDto={}",resourceDto);
        resourceService.updateResource(resourceDto);
        return ResponseResult.success();
    }

    /**
     * 修改资源启用禁用状态
     * @param resourceDto
     * @return
     */
    @PostMapping("/enable")
    @ApiOperationSupport(includeParameters = {"resourceDto.dataState","resourceDto.resourceNo","resourceDto.parentResourceNo"})
    public ResponseResult updateStatus(@RequestBody ResourceDto resourceDto){
        log.info("修改资源启用禁用状态接口,入参={}",resourceDto);
        resourceService.updateStatus(resourceDto);
        return ResponseResult.success();
    }

    /**
     * 删除菜单
     * @return
     */
    @DeleteMapping("/{menuId}")
    public ResponseResult deleteResourceById(@PathVariable String menuId){
        log.info("删除菜单资源接口，入参menuId={}",menuId);
        resourceService.deleteResourceById(menuId);
        return ResponseResult.success();
    }

    /**
     * 获取当前用户动态菜单资源
     * @return
     */
    @GetMapping("/menus")
    public ResponseResult<List<MenuVo>> getMenusResource(){
        log.info("获取动态菜单资源接口");
        List<MenuVo> menuVo = resourceService.getMenusResource();
        return ResponseResult.success(menuVo);
    }
}
